package com.student.management.root.dao.impl;

import com.student.management.root.bean.Menu;
import com.student.management.root.bean.Role;
import com.student.management.root.dao.RoleDao;
import com.student.management.root.utils.DBUtils;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class RoleDaoImpl extends DBUtils implements RoleDao {
    @Override
    public List<Role> getRoleList(int pageIndex, int pageSize) {
        List rl = new ArrayList();
        String sql = "SELECT * FROM role WHERE 1=1 limit ?,?;";
        List params = new ArrayList<>();
        params.add((pageIndex - 1) * pageSize);
        params.add(pageSize);
        resultSet = query(sql, params);
        if (resultSet == null) {
            return null;
        }
        try {
            while (resultSet.next()) {
                Role role = new Role();
                role.setRoleid(resultSet.getInt("roleid"));
                role.setRolename(resultSet.getString("rolename"));
                role.setRolestate(resultSet.getInt("rolestate"));
                rl.add(role);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            closeAll();
        }
        return rl;
    }

    @Override
    public int total() {
        int total = 0;
        try {
            String sql = "select count(1) from role r  ";
            resultSet = query(sql, null);
            while (resultSet.next()) {
                total = resultSet.getInt(1);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            closeAll();
        }
        return total;
    }

    @Override
    public int insert(Role role) {
        int key = 0; //新增数据的id
        try {
            String sql = "insert into role values(null,?,?)";
            List params = new ArrayList();
            params.add(role.getRolename());
            params.add(role.getRolestate());
            int update = update(sql, params);//update变量保存的是受影响的行数

            ResultSet generatedKeys = pps.getGeneratedKeys();
            if (generatedKeys.next()) {
                key = generatedKeys.getInt(1);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            closeAll();
        }
        return key;
    }

    @Override
    public Role findbyid(int roleid) {
        Role role = new Role();
        List menuList = new ArrayList();
        try {
            String sql = "select * from role r,menu m,middle mid where r.roleid=mid.roleid and mid.menuid=m.menuid and r.roleid=?";
            List params = new ArrayList();
            params.add(roleid);

            resultSet = query(sql, params);
            while (resultSet.next()) {
                role.setRoleid(resultSet.getInt("roleid"));
                role.setRolename(resultSet.getString("rolename"));
                role.setRolestate(resultSet.getInt("rolestate"));

                Menu menu = new Menu();
                menu.setMenuid(resultSet.getInt("menuid"));
                menu.setMenuname(resultSet.getString("menuname"));
                menu.setUrl(resultSet.getString("url"));
                menu.setState(resultSet.getInt("state"));
                menu.setUpmenuid(resultSet.getInt("upmenuid"));
                menuList.add(menu);
            }
            role.setMenuList(menuList);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            closeAll();
        }
        return role;
    }

    @Override
    public int updateById(Role role) {
        int update = 0;
        try {
            //这里需要大家自己完善非主键列以外的列的更新
            String sql = "UPDATE role SET rolename=?,rolestate=? WHERE roleid=?;";
            List params = new ArrayList();
            params.add(role.getRolename());
            params.add(role.getRolestate());
            params.add(role.getRoleid());
            update = update(sql, params);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            closeAll();
        }
        return update;
    }

    @Override
    public Role queryRoleByRid(int roleid) {
        Role role = new Role();
        try {
            String sql = "select * from role where roleid = ?;";
            List params = new ArrayList<>();
            params.add(roleid);
            resultSet = query(sql, params);
            while (resultSet.next()) {
                role.setRoleid(resultSet.getInt("roleid"));
                role.setRolename(resultSet.getString("rolename"));
                role.setRolestate(resultSet.getInt("rolestate"));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            closeAll();
        }
        return role;
    }

    @Override
    public int delete(int roleId) {
        int del = 0;
        try {
            String sql = "delete  from role where roleid = ?;";
            List params = new ArrayList<>();
            params.add(roleId);
            del = update(sql, params);

        } finally {
            closeAll();
        }
        return del;
    }

    @Override
    public int enableRole(int roleid) {
        int enableNum = 0;
        String sql = "UPDATE role SET rolestate=1 WHERE roleid=?;";
        List params = new ArrayList<>();
        params.add(roleid);
        enableNum = update(sql, params);
        return enableNum;
    }
}
